package com.wang.study.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.wang.study.Proxy.JdkDynamicProxyUtil;
import com.wang.study.commons.BaseServlet;
import com.wang.study.model.Goods;
import com.wang.study.service.GoodsService;
import com.wang.study.service.impl.GoodsServiceImp;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/goods.do")
public class GoodsController extends BaseServlet {
    private GoodsService goodsService= new JdkDynamicProxyUtil<GoodsService>().getProxyObject(new GoodsServiceImp());

    /**
     * 通过大类信息查询商品
     * @param req
     * @param resp
     */
    public void findGoodsByPageNum(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String pageNumStr = req.getParameter("pageNum");
        String pageSizeStr = req.getParameter("pageSize");
        //判断如果是第一次分页，没有参数则默认1,3
        Integer pageNum = pageNumStr==null?1:Integer.valueOf(pageNumStr);
        Integer pageSize = pageSizeStr==null?3:Integer.valueOf(pageSizeStr);

        PageInfo<Goods> pageInfo = goodsService.findGoodsByPageNum(pageNum,pageSize);
        resp.getWriter().write(new JSONObject().toJSONString(pageInfo));
    }
    /**
     * 通过大类信息查询商品
     * @param req
     * @param resp
     */
    public void findGoodsByCid(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String cid = req.getParameter("cid");
        String pageNumStr = req.getParameter("pageNum");
        String pageSizeStr = req.getParameter("pageSize");
        //判断如果是第一次分页，没有参数则默认1,3
        Integer pageNum = pageNumStr==null?1:Integer.valueOf(pageNumStr);
        Integer pageSize = pageSizeStr==null?3:Integer.valueOf(pageSizeStr);

        PageInfo<Goods> pageInfo = goodsService.findGoodsByCid(Integer.valueOf(cid),pageNum,pageSize);
        resp.getWriter().write(new JSONObject().toJSONString(pageInfo));
    }

    /**
     * 通过id找商品详情
     * @param req
     * @param resp
     */
    public void selectGoodById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String gid = req.getParameter("gid");
        Goods  good= goodsService.selectGoodById(gid);
        resp.getWriter().write(new JSONObject().toJSONString(good));
    }
    /**
     * 找全部商品
     * @param req
     * @param resp
     */
    public void findGoodsAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //判断如果是第一次分页，没有参数则默认1,3
        String pageNumStr = req.getParameter("pageNum");
        String pageSizeStr = req.getParameter("pageSize");
        Integer pageNum = pageNumStr==null?1:Integer.valueOf(pageNumStr);
        Integer pageSize = pageSizeStr==null?3:Integer.valueOf(pageSizeStr);

        PageInfo<Goods> pageInfo = goodsService.findGoodsAll(pageNum,pageSize);
        resp.getWriter().write(new JSONObject().toJSONString(pageInfo));
    }

    /**
     * 查询所有
     * @param req
     * @param resp
     * @throws IOException
     */
    public void findGoodsAll1(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<Goods> pageInfo = goodsService.findGoodsAll1();
        resp.getWriter().write(new JSONObject().toJSONString(pageInfo));
    }
    /**
     * 通过id删除该商品
     * @param req
     * @param resp
     * @throws IOException
     */
    public void delGoodsById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        Integer i = goodsService.delGoodsById(Integer.valueOf(id));
        resp.getWriter().write(new JSONObject().toJSONString(i));
    }/**
     * 通过id删除该商品
     * @param req
     * @param resp
     * @throws IOException
     */
    public void addGoods(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        String author = req.getParameter("author");
        String publisher = req.getParameter("publisher");
        String publishtime = req.getParameter("publishtime");
        String decription = req.getParameter("decription");
        String image = req.getParameter("image");
        String salesprice = req.getParameter("salesprice");
        String status = req.getParameter("status");
        String uptime = req.getParameter("uptime");
        Integer i = goodsService.addGoods(Integer.valueOf(id),author,publisher,publishtime,decription,image,salesprice,status,uptime);
        resp.getWriter().write(new JSONObject().toJSONString(i));
    }

    /**
     * 更新图片路径和上传图片
     */
    public String updateGoodById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        String imagePath = req.getParameter("imagePath");
        System.out.println(imagePath);
        goodsService.updateGoodById(Integer.valueOf(id),"/images/"+imagePath);
        return "r:/admin/goodsManage.jsp";
    }

    /**
     * 模糊查询
     * @param req
     * @param resp
     */
    public void likeSearch (HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String name = req.getParameter("name");
        List<Goods> goods = goodsService.likeSearch(name+"%");
        resp.getWriter().write(new JSONObject().toJSONString(goods));
    }
}
